#! /usr/bin/env python
#coding=utf-8

"""

    Simple class to display high scores 

"""

import pygame
import tools
import os
import strings
import menu
import consts

from hslocal import HSLocal
from math import sin, cos, pi

class HSDrawer(object):
    
    def __init__(self, hs):        
        # init fonts
        fontPath = os.path.join('..', 'font', 'freesansbold.ttf')        
        self.smallfont = pygame.font.Font(fontPath, 13)
        self.mHS = hs
        self.mTableName = None        
        fontPath = os.path.join('..', 'font', 'agentred.ttf')
        self.mTitleFont = pygame.font.Font(fontPath, 18)
        self.mTitleSz = self.mTitleFont.size(strings.STR_MAIN_MENU_HSCORE)
        
        # back is also a rotating menu but it consists of only one button :)
        self.mBack = menu.RotatingMenu(x = 320, y = 240, radius = 220, arc = pi, defaultAngle = pi / 2.0)
        self.mBack.addItem(menu.MenuItem(strings.STR_MENU_BACK))            
        self.mBack.selectItem(0)
        
    def useTable(self, tableName):
        self.mTableName = tableName
        self.mBack.update()
    
    def processEvents(self, events, parent):
        for event in events:
            if event.type == pygame.MOUSEBUTTONDOWN and event.button == consts.LEFT_MOUSE_BUTTON:
                # check if we clicked a mouse over any of menu items
                m_x, m_y = event.pos
                for item in self.mBack.items:
                    metrics = item.getMetrics()
                    if m_x >= metrics[0] and m_y >= metrics[1] and m_x <= metrics[2] and m_y <= metrics[3]:
                        parent.showScoreTypeMenu()
                        return True
            if event.type == pygame.KEYDOWN:                    
                if event.key == pygame.K_RETURN or event.key == pygame.K_ESCAPE:
                    parent.showScoreTypeMenu()
                    return True
        return False
    
    def draw(self, surf, scrWidth, scrHeight):
        if self.mTableName != None:
            
            x, y = scrWidth / 2, 10
            it = self.mHS.getScoreIter(self.mTableName)
            tools.drawTextAligned(surf, self.mTitleFont, strings.STR_MAIN_MENU_HSCORE, x, y, (255, 255, 255), tools.HORIZ_CENTER | tools.VERT_TOP)
            y += self.mTitleSz[1] + 5
            tools.drawTextAligned(surf, self.smallfont, 'Top ' + str(HSLocal.SCORES_COUNT) + ' Scores (' + str(self.mTableName) + ')', x, y, (0, 100, 255), tools.HORIZ_CENTER | tools.VERT_TOP)
            y += self.mTitleSz[1]
            
            idx = 0            
            while idx < HSLocal.SCORES_COUNT:
                try:
                    item = it.next()
                    tools.drawTextAligned(surf, self.smallfont, str(item[0]), x - self.mTitleSz[0] / 2, y, (255, 255, 255))
                    tools.drawTextAligned(surf, self.smallfont, str(item[1]), x + self.mTitleSz[0] / 2, y, (0, 255, 0), tools.HORIZ_RIGHT | tools.VERT_TOP)
                except StopIteration:
                    tools.drawTextAligned(surf, self.smallfont, 'empty', x - self.mTitleSz[0] / 2, y, (100, 100, 100))
                    tools.drawTextAligned(surf, self.smallfont, '0', x + self.mTitleSz[0] / 2, y, (0, 100, 0), tools.HORIZ_RIGHT | tools.VERT_TOP)
                y += 15
                idx += 1
                
        self.mBack.draw(surf)
        